package com.turboconnard.display;

import java.util.Vector;

import android.view.Display;

import com.turboconnard.core.FPS;
import com.turboconnard.core.MainActivity;
import com.turboconnard.events.Event;

public class Stage extends Sprite {
	private static Stage _instance;
	public static int PIXELUNIT;
	public int width;
	public int height;
	private double amountShakeX;
	private double stepShakeX;
	private double amountShakeY;
	private double stepShakeY;
	public float correction;
	//private int unit;
	public MainActivity activity;
	
	/**
	 * Stage is the 2D projection of the 3D scene
	 * it's your phone width / height	
	 * 
	 * the third parameter is the the size in pixel for one 3d unit.
	 * if you create a 64x64 texture and apply it to a 2x2 plane at z=0,
	 * your plane will be displayed "pixel perfect" on the screen with the param 64.  
	 *  
	 *  
	 * @param w the scene width in pixel
	 * @param h the scene height in pixel
	 * @param u
	 */
	public Stage(int w,int h,int u){
		_instance = this;
		width = w;
		height = h;
		PIXELUNIT = u;
		
		correction = 2f/320f;// height /(height)*2/height; 		
	}
	public void resize(int w,int h){
		width = w;
		height = h;
		correction = 2f/320f;//1/height;// ( (float)( height /(float) (PIXELUNIT))*2)/(float) height;
	}
	public static Stage getInstance(){
		return _instance;
	}
	/**
	 * 
	 * @return int 1 Landscape, 0 portrait
	 */
	public int getOrientation(){
		Display getOrient = activity.getWindowManager().getDefaultDisplay();
		return getOrient.getOrientation();
	}
	/**
	 * 
	 * @param amountX
	 * @param amountY
	 * @param time
	 */
	public void shake(int amountX,int amountY,int time){
        amountShakeX = amountX;
        amountShakeY = amountY;
        stepShakeX = amountShakeX/( time*60 );
        stepShakeY = amountShakeY/(time*60 );
        FPS.getInstance().addEventListener(Event.ENTER_FRAME,this );		
	}
	@Override
	public void event(Event e){
		if(e.name == Event.ENTER_FRAME){
			x = (float)( Math.cos( Math.random()*(Math.PI*2))*amountShakeX );
            y = (float)( Math.cos( Math.random()*(Math.PI*2))*amountShakeY );
            amountShakeX -= stepShakeX;
            amountShakeY -= stepShakeY;
          
             if(amountShakeX<=0)  x = 0;
             if(amountShakeY<=0)  y = 0;           
             if(amountShakeX<=0 && amountShakeY<=0){
            	//removeEventListener(this);
             }			
		}
	}
	
}
